package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.domain.SystemDictionary;
import cn.JHai.rbac.domain.SystemDictionaryItem;
import cn.JHai.rbac.mapper.SystemDictionaryItemMapper;
import cn.JHai.rbac.mapper.SystemDictionaryMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.SystemDictionaryItemService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemImp implements SystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    public int Delete(Long id) {
        return systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SystemDictionaryItem get(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> findAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(SystemDictionaryItem systemDictionaryItem , Long parent_id) {
        //是插入

        if(systemDictionaryItem.getId()==null){
            systemDictionaryItem.setParent_id(parent_id);
            return systemDictionaryItemMapper.insert(systemDictionaryItem);
        }else {
            //跟新
            systemDictionaryItem.setParent_id(parent_id);
            return systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        Long count = systemDictionaryItemMapper.selectForCount(queryObject);
        List<SystemDictionaryItem> list =systemDictionaryItemMapper.pageSelect(queryObject);

        for (SystemDictionaryItem sdi : list) {
            JSONObject so = new JSONObject();
            so.put("id",sdi.getId());so.put("title",sdi.getTitle());so.put("sequence",sdi.getSequence());
            sdi.setJson(so);
        }
        /*if(count<=3l){
            //systemDictionaryItems = systemDictionaryItemMapper.selectAll();
        }
        else{
            //systemDictionaryItems = systemDictionaryItemMapper.pageSelect(queryObject);
        }*/
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }

    @Override
    public PageResult queryByParentId(QueryObject queryObject,Long parent_id) {
        Long count = systemDictionaryItemMapper.selectForCountByParentId(queryObject,parent_id);
        List<SystemDictionaryItem> list =systemDictionaryItemMapper.pageSelectByParentId(queryObject,parent_id);
        for (SystemDictionaryItem sdi : list) {
            JSONObject so = new JSONObject();
            so.put("sequence", sdi.getSequence());
            so.put("id", sdi.getId());
            so.put("title", sdi.getTitle());
            sdi.setJson(so);
        }
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }
}
